package com.zbbt.sportslottery.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.AwardNoticeNewDetail;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.DrawDetailAdapter;
import com.zbbt.sportslottery.ui.widget.DrawListView;
import com.zbbt.sportslottery.util.TimeUtil;

public class DrawHisoryDetailActivity extends SimpleBaseActivity {
	private String game_code;
	private String game_id;
	private String draw;
	private String number;
	private String number2;
	private String date;
	private String name;
	private String areaid;
	private String type;
	private DrawListView listview1;
	private DrawDetailAdapter adapter;
	private TextView top_phase;
	private TextView top_date;
	private TextView top_text;
	private TextView sellnumber;
	private TextView sellmoney;
	private TextView[] topball = new TextView[8];
	private TextView[] matchball = new TextView[14];
	private LinearLayout balllayout;
	private LinearLayout matchlayout;
	private String name1105 = "任选二,任选三,任选四,任选五,任选六,任选七,任选八,前一直选,前二直选,前二组选,前三直选,前三组选";
	private String money1105 = "6,19,78,540,90,26,9,13,130,65,1170,195";

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_superhistory_detail);
		Intent intent = getIntent();
		game_code = intent.getStringExtra("code");
		game_id = intent.getStringExtra("id");
		date = intent.getStringExtra("date");
		name = intent.getStringExtra("name");
		number = intent.getStringExtra("number");
		number2 = intent.getStringExtra("number2");
		draw = intent.getStringExtra("draw");
		areaid = intent.getStringExtra("areaid");
		type = intent.getStringExtra("type");
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		balllayout = (LinearLayout) findViewById(R.id.top_linearlayout);
		matchlayout = (LinearLayout) findViewById(R.id.match_linearlayout);
		listview1 = (DrawListView) findViewById(R.id.listview1);
		top_text = (TextView) findViewById(R.id.top_text);
		top_phase = (TextView) findViewById(R.id.top_phase);
		top_date = (TextView) findViewById(R.id.top_date);
		sellnumber = (TextView) findViewById(R.id.sellnumber);
		sellmoney = (TextView) findViewById(R.id.sellmoney);
		if (type.equals("3")) {
			balllayout.setVisibility(View.GONE);
			matchlayout.setVisibility(View.VISIBLE);
			matchball[0] = (TextView) findViewById(R.id.match_ball0);
			matchball[1] = (TextView) findViewById(R.id.match_ball1);
			matchball[2] = (TextView) findViewById(R.id.match_ball2);
			matchball[3] = (TextView) findViewById(R.id.match_ball3);
			matchball[4] = (TextView) findViewById(R.id.match_ball4);
			matchball[5] = (TextView) findViewById(R.id.match_ball5);
			matchball[6] = (TextView) findViewById(R.id.match_ball6);
			matchball[7] = (TextView) findViewById(R.id.match_ball7);
			matchball[8] = (TextView) findViewById(R.id.match_ball8);
			matchball[9] = (TextView) findViewById(R.id.match_ball9);
			matchball[10] = (TextView) findViewById(R.id.match_ball10);
			matchball[11] = (TextView) findViewById(R.id.match_ball11);
			matchball[12] = (TextView) findViewById(R.id.match_ball12);
			matchball[13] = (TextView) findViewById(R.id.match_ball13);
			init3();
		} else {
			topball[0] = (TextView) findViewById(R.id.top_ball0);
			topball[1] = (TextView) findViewById(R.id.top_ball1);
			topball[2] = (TextView) findViewById(R.id.top_ball2);
			topball[3] = (TextView) findViewById(R.id.top_ball3);
			topball[4] = (TextView) findViewById(R.id.top_ball4);
			topball[5] = (TextView) findViewById(R.id.top_ball5);
			topball[6] = (TextView) findViewById(R.id.top_ball6);
			topball[7] = (TextView) findViewById(R.id.top_ball7);
			init2();
		}

	}

	private void init3() {
		// TODO Auto-generated method stub
		String[] match = number.split(",");
		int matchlength = match.length;
		for (int i = 0; i < matchlength; i++) {
			matchball[i].setText(match[i]);
		}
		for (int i = matchlength; i < 14; i++) {
			matchball[i].setVisibility(View.GONE);
		}
	}

	private void init2() {
		// TODO Auto-generated method stub
		String[] redball = number.split(",");
		int redlength = redball.length;
		for (int i = 0; i < redlength; i++) {
			topball[i].setText(redball[i]);
		}
		if (number2 != null && !number2.equals("null")) {
			String[] blueball = number2.split(",");
			if (blueball.length > 0) {
				for (int i = 0; i < blueball.length; i++) {
					topball[redlength + i].setText(blueball[i]);
					topball[redlength + i].setBackgroundResource(R.drawable.blueball);
				}
				for (int i = blueball.length + redlength; i < 8; i++) {
					topball[i].setVisibility(View.GONE);
				}
			}
		} else {
			for (int i = redlength; i < 8; i++) {
				topball[i].setVisibility(View.GONE);
			}
		}
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onClickEvent(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		top_text = (TextView) findViewById(R.id.top_text);
		top_phase = (TextView) findViewById(R.id.top_phase);
		top_date = (TextView) findViewById(R.id.top_date);
		top_text.setText(name);
		top_phase.setText("第" + draw + "期");
		top_date.setText(TimeUtil.getTimeDate(Long.parseLong(date)));
		if (game_code.equals("1105")) {
			findViewById(R.id.table_1).setVisibility(View.GONE);
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			View mView = inflater.inflate(R.layout.item_draw_head2, null);
			listview1.addHeaderView(mView);
			String[] name = name1105.split(",");
			String[] money = money1105.split(",");
			adapter = new DrawDetailAdapter(context, name, null, money, 2);
			listview1.setAdapter(adapter);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
			layoutParams.setMargins(16, 0, 16, 0);
			listview1.setLayoutParams(layoutParams);
		} else {
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			View mView = inflater.inflate(R.layout.item_draw_head, null);
			listview1.addHeaderView(mView);
			JsonObject jsonObject = new JsonObject();
			jsonObject.addProperty("lotteryCode", game_code);
			jsonObject.addProperty("lotteryId", game_id);
			jsonObject.addProperty("drawId", draw);
			jsonObject.addProperty("lotteryType", "2");
			jsonObject.addProperty("areaId", areaid);
			ZbHttpClient.postNewApi(ServerConfig.API_OPEN_DETAIL, jsonObject, new TypeGsonHttpHandler<SuperResult<AwardNoticeNewDetail>>() {
				@Override
				public void onSuccess(SuperResult<AwardNoticeNewDetail> data2) {
					// TODO Auto-generated method stub
					if (data2 != null && data2.getData() != null) {
						String numbers = "--", moneys = "--";
						if (data2.getData().get(0).getSalesAmount() != null && !data2.getData().get(0).getSalesAmount().equals("null")) {
							numbers = data2.getData().get(0).getSalesAmount().replace(" ", "");
							if (numbers.contains(".")) {
								numbers = numbers.substring(0, numbers.indexOf("."));
							}
						}
						if (data2.getData().get(0).getJackpotBonus() != null && !data2.getData().get(0).getJackpotBonus().equals("null")) {
							moneys = data2.getData().get(0).getJackpotBonus().replace(" ", "");
							if (moneys.contains(".")) {
								moneys = moneys.substring(0, moneys.indexOf("."));
							}
						}
						sellnumber.setText(numbers);
						sellmoney.setText(moneys);
						AwardNoticeNewDetail data = data2.getData().get(0);
						if (data.getPrize() != null && data.getNoteSum() != null && data.getNoteBonus() != null) {
							String[] name = data.getPrize().split(",");
							String[] number = data.getNoteSum().split(",");
							String[] money = data.getNoteBonus().split(",");
							if (name.length == number.length && name.length == money.length) {
								adapter = new DrawDetailAdapter(context, name, number, money, 3);
								listview1.setAdapter(adapter);
							}
						}
					}
				}

				@Override
				public void onFailure(String errorCode, String errorMsg) {
					// TODO Auto-generated method stub
				}
			});
		}
	}
}
